﻿using System.Collections.Generic;
using System.Linq;
using AccelerateSensor.Database.SQLite;

namespace AccelerateSensor.Service.DbProxy.SQLite.Dal
{
    internal class GatewayPrivateAttribute
    {
        private static readonly string TableName = SqliteHelper.TableGatewayPrivateAttribute;

        /// <summary>
        /// 添加网关私有属性
        /// </summary>
        /// <param name="model">网关私有属性对象</param>
        /// <returns>Rid</returns>
        public int Insert(Models.GatewayPrivateAttribute model)
        {
            if (model == null)
            {
                return 0;
            }

            var sql = $"insert into {TableName}(GatewayUuid, StartFreq, AntennaGain, RfChNum, ChannelDataRate) " +
                      "values(@GatewayUuid, @StartFreq, @AntennaGain, @RfChNum, @ChannelDataRate);";
            return SqliteDbHelper.Insert(sql, model);
        }

        /// <summary>
        /// 批量添加网关私有属性
        /// </summary>
        /// <param name="models">网关私有属性对象集合</param>
        /// <returns>true成功，false失败</returns>
        public bool Insert(List<Models.GatewayPrivateAttribute> models)
        {
            if (models == null || models.Count == 0)
            {
                return false;
            }

            var sql = $"insert into {TableName}(GatewayUuid, StartFreq, AntennaGain, RfChNum, ChannelDataRate) " +
                      "values(@GatewayUuid, @StartFreq, @AntennaGain, @RfChNum, @ChannelDataRate);";
            return SqliteDbHelper.Insert(sql, models.ToList<object>());
        }

        /// <summary>
        /// 更新网关私有属性(不修改网关私有属性Id)
        /// </summary>
        /// <param name="model">网关私有属性对象</param>
        /// <returns>true成功，false失败</returns>
        public bool Update(Models.GatewayPrivateAttribute model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set GatewayUuid = @GatewayUuid, StartFreq = @StartFreq, AntennaGain = @AntennaGain, " +
                      "    RfChNum = @RfChNum, ChannelDataRate = @ChannelDataRate " +
                      "where Rid = @Rid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 通过网关编号更新网关私有属性
        /// </summary>
        /// <param name="model">网关私有属性对象</param>
        /// <returns>true成功，false失败</returns>
        public bool UpdateByGatewayUuid(Models.GatewayPrivateAttribute model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set StartFreq = @StartFreq, AntennaGain = @AntennaGain, " +
                      "    RfChNum = @RfChNum, ChannelDataRate = @ChannelDataRate " +
                      "where GatewayUuid = @GatewayUuid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 删除网关私有属性
        /// </summary>
        /// <param name="model">网关私有属性对象</param>
        /// <returns>true成功，false失败</returns>
        public bool Delete(Models.GatewayPrivateAttribute model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"delete from {TableName} where Rid = @Rid";
            return SqliteDbHelper.Delete(sql, model);
        }

        /// <summary>
        /// 查询网关私有属性
        /// </summary>
        /// <param name="rid">网关私有属性Id</param>
        /// <returns>网关私有属性对象</returns>
        public Models.GatewayPrivateAttribute GetModel(int rid)
        {
            var sql = $"select * from {TableName} where Rid = @Rid";
            var model = new Models.GatewayPrivateAttribute() { Rid = rid };
            return SqliteDbHelper.GetDataList<Models.GatewayPrivateAttribute>(sql, model).FirstOrDefault();
        }

        /// <summary>
        /// 查询网关私有属性
        /// </summary>
        /// <param name="gatewayUuid">网关Id</param>
        /// <returns>网关私有属性对象</returns>
        public Models.GatewayPrivateAttribute GetModel(string gatewayUuid)
        {
            var sql = $"select * from {TableName} where GatewayUuid = @GatewayUuid";
            var model = new Models.GatewayPrivateAttribute() { GatewayUuid = gatewayUuid };
            return SqliteDbHelper.GetDataList<Models.GatewayPrivateAttribute>(sql, model).FirstOrDefault();
        }

        /// <summary>
        /// 查询所有网关私有属性
        /// </summary>
        /// <returns>网关私有属性对象集合</returns>
        public List<Models.GatewayPrivateAttribute> GetAllModels()
        {
            var sql = $"select * from {TableName}";
            return SqliteDbHelper.GetDataList<Models.GatewayPrivateAttribute>(sql);
        }
    }
}
